import QRequest from '@/service/index';
import { generateNameToLevel } from '@/config/constant';
import {
    OrderUpdate,
    PlanningData,
    SchedulePageParams,
    SchedulePageResponse,
    PlaningOperationParams,
    OperationProgressResponse,
    PlanningOperationParamResponse
} from './model/type';

/**
 * @: 分页查询排程运算列表
 */
export const doGetScheduleList = (data: SchedulePageParams) => {
    return QRequest.post<SchedulePageResponse>(`/schedule`, data);
};

/**
 * @: 分页查询订单列表
 */
export const doGetOrderList = (data: SchedulePageParams) => {
    return QRequest.post<SchedulePageResponse>(`/schedule`, data, {
        headers: {
            loadingTarget: `#${generateNameToLevel('1')}`
        }
    });
};

/**
 * @: 更新订单优先级
 */
export const doUpdateOrderPriority = (data: { id: number; orderPriority: number }) => {
    return QRequest.put(`/schedule/update/increment`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 编辑订单
 */
export const doUpdateSchedule = (data: OrderUpdate) => {
    return QRequest.put(`/schedule/update`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 删除订单数据
 */
export const doDelSchedule = (data: number[]) => {
    return QRequest.delete(`/schedule/delete`, { data });
};

/**
 * @: 开始计划运算
 */
export const doPlaningOperation = (data: PlaningOperationParams) => {
    return QRequest.post(`/schedule/mrpPlaningOperation`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 计划运算进度
 */
export const doGetPlanOperationProgress = (params: { id: string }) => {
    return QRequest.get<OperationProgressResponse>(`/schedule/planOperationProgress`, {
        params,
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 获取排程基础参数
 */
export const doGetPlanningOperationParam = () => {
    return QRequest.get<PlanningOperationParamResponse>(`/globalSettingsForScheduling/getOne`);
};

/**
 * @: 更新排程基础参数
 */
export const doPutPlanningOperationParam = (data: PlanningData) => {
    return QRequest.put<PlanningOperationParamResponse>(
        `/globalSettingsForScheduling/update`,
        data
    );
};

/**
 * @: 检查订单内是否有采购计划
 */
export const doCheckPurchasePlan = (params: { orderNumbers: string }) => {
    return QRequest.get(`/schedule/checkPurchasePlan`, {
        params,
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 释放占用物料
 */
export const doReleaseMaterial = (params: { id: string }) => {
    return QRequest.put(`/schedule/release/occupation/material`, null, {
        params,
        headers: {
            hideLoading: 1
        }
    });
};
